package com.intertid.mongodb.test
import org.bson.types.ObjectId
import com.intertid.mongodb.annotations.ID
import org.apache.commons.beanutils.PropertyUtils
import com.intertid.mongodb.commons.MongoDBUtils
import com.intertid.mongodb.Mapper
import com.intertid.scala.commons._
import java.util.ArrayList
import com.intertid.mongodb.MongoDBTemplate
import com.mongodb.Mongo
import com.mongodb.BasicDBList
import com.mongodb.DBRef
import net.sf.cglib.beans.BeanGenerator
import org.objectweb.asm.ClassVisitor
import net.sf.cglib.core.ClassEmitter
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
import net.sf.cglib.core.EmitUtils
import net.sf.cglib.core.Constants
import net.sf.cglib.proxy.Enhancer
import com.intertid.mongodb.LazyLoadDynamicProxy
import net.sf.cglib.proxy.MethodInterceptor
import com.intertid.mongodb.ProxyTag
import java.lang.reflect.Method
import net.sf.cglib.proxy.MethodProxy

object TestProgram extends App {

  Mapper.init("com.intertid.mongodb.test");
  Mapper.classinfos.foreach(ci =>

    ci.fields.foreach {
      f => println(ci.entityClass.getName() + "." + f.getName);
    });

  val enhancer = new Enhancer;
  enhancer.setSuperclass(classOf[User]);
  enhancer.setInterfaces(Array(classOf[ProxyTag]));

  enhancer.setCallback(new MethodInterceptor{
    override def intercept(proxy: Object, method: Method, params: Array[Object], methodProxy: MethodProxy): Object = {
      println("run " + method.getName())

      return null;
    }
  });

  val user=enhancer.create().asInstanceOf[User];
  user.username="huhong";
  println(user.username);

  //  val template = new MongoDBTemplate(new Mongo(), "test");
  //  val id = addTestBean().getId();
  //  val found = template("User") findOneByID (id)
  //  val bean = MongoDBUtils.dbObject2Bean(found.get).asInstanceOf[User];
  //
  //  template -= bean;
  //
  //  
  //
  //  template.connection.close();
  //  def addTestBean() = {
  //
  //    val d = new Dept;
  //    d.setDeptName("");
  //
  //    template += d;
  //    val r = new Role();
  //    r.setName("administrators");
  //
  //    template += r;
  //    val u = new User;
  //    u.username = "huhong";
  //    u.password = "123456";
  //    u.dept = d;
  //    u.roles = List(r);
  //    template += u;
  //
  //    u;
  //  }
}